import string

import numpy as np
import cv2

global rect
global leftButtonDown
global leftButtonUp
# 鼠标事件的回调函数
def on_mouse(event, x, y, flag, param):


    # 鼠标左键按下
    global leftButtonDown, leftButtonUp,rect
    if event == cv2.EVENT_LBUTTONDOWN:
        rect[0] = x
        rect[2] = x
        rect[1] = y
        rect[3] = y
        leftButtonDown = True
        leftButtonUp = False

    # 移动鼠标事件
    if event == cv2.EVENT_MOUSEMOVE:
        if leftButtonDown and not leftButtonUp:
            rect[2] = x
            rect[3] = y

            # 鼠标左键松开
    if event == cv2.EVENT_LBUTTONUP:
        if leftButtonDown and not leftButtonUp:
            x_min = min(rect[0], rect[2])
            y_min = min(rect[1], rect[3])

            x_max = max(rect[0], rect[2])
            y_max = max(rect[1], rect[3])

            rect[0] = x_min
            rect[1] = y_min
            rect[2] = x_max
            rect[3] = y_max
            leftButtonDown = False
            leftButtonUp = True

def getSkin(pic:int,site:int):#通过鼠标绘制矩形进行皮肤提取，pic图片名,site位置，1 2 3分别是脖子 左手 右手
    img = cv2.imread('./add_background/front/0'+str(pic)+'.jpg')
    mask = np.zeros(img.shape[:2], np.uint8)
    global leftButtonDown,leftButtonUp,rect
    bgdModel = np.zeros((1, 65), np.float64)  # 背景模型
    fgdModel = np.zeros((1, 65), np.float64)  # 前景模型
    rect = [0, 0, 0, 0]  # 设定需要分割的图像范围

    leftButtonDown = False  # 鼠标左键按下
    leftButtonUp = True  # 鼠标左键松开

    cv2.namedWindow('img')  # 指定窗口名来创建窗口
    cv2.setMouseCallback('img', on_mouse)  # 设置鼠标事件回调函数 来获取鼠标输入
    cv2.imshow('img', img)  # 显示图片

    while cv2.waitKey(2) == -1:
        # 左键按下，画矩阵
        if leftButtonDown and not leftButtonUp:
            img_copy = img.copy()
            cv2.rectangle(img_copy, (rect[0], rect[1]), (rect[2], rect[3]), (0, 255, 0), 2)

            cv2.imshow('img', img_copy)

        # 左键松开，矩形画好
        elif not leftButtonDown and leftButtonUp and rect[2] - rect[0] != 0 and rect[3] - rect[1] != 0:
            rect[2] = rect[2] - rect[0]
            rect[3] = rect[3] - rect[1]
            rect_copy = tuple(rect.copy())
            rect = [0, 0, 0, 0]
            # 物体分割
            cv2.grabCut(img, mask, rect_copy, bgdModel, fgdModel, 1, cv2.GC_INIT_WITH_RECT)

            mask2 = np.where((mask == 2) | (mask == 0), 0, 1).astype('uint8')
            img_show = img * mask2[:, :, np.newaxis]
            # 显示图片分割后结果--显示原图
            cv2.imshow('grabcut', img_show)
            cv2.imshow('img', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    cv2.imwrite("./skins/0"+str(pic)+'_'+str(site)+'.jpg',img_show)
def combine_skin(pic:int):
    img1 = cv2.imread('./skins/0' + str(pic) + '_1.jpg')
    img2 = cv2.imread('./skins/0' + str(pic) + '_2.jpg')
    img3 = cv2.imread('./skins/0' + str(pic) + '_3.jpg')
    img=img1+img2+img3
    cv2.imwrite("./skins/0"+str(pic)+'.jpg',img)
    bg=cv2.imread('./res/0'+str(pic)+'_res.jpg')
    lower = np.array([0, 0, 0])
    upper = np.array([5, 5, 5])
    mask = cv2.inRange(img, lower, upper)
    masked_image = np.copy(img)
    crop_background = np.copy(bg)
    crop_background[mask == 0] = [0, 0, 0]
    complete_image = masked_image + crop_background
    cv2.imshow("res",complete_image)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    path='./res/0'+str(pic)+'_UpSkin.jpg'
    cv2.imwrite(path,complete_image)
for i in range(1,6):
    for j in range(1,4):
        getSkin(i,j)
    combine_skin(i)
